<?php


namespace services\linkedcare;

use common\helpers\ArrayHelper;
use common\models\linkedcare\LinkedcareChannel;
use Yii;
use common\components\Service;

class LinkedcareChannelService extends Service
{
    /**
     * E看牙渠道下拉筛选项
     * @return array[]
     */
    public static function getChannelCondition($isTree): array
    {
        $data = LinkedcareChannel::find()
            ->select(['id', 'parent_id pid', 'source_name', 'status'])
            ->where(['status' => [LinkedcareChannel::STATUS_INVALID, LinkedcareChannel::STATUS_ENABLE]])
            ->asArray()
            ->all();
        if ($isTree) {
            foreach ($data as &$row) {
                $row['expand'] = false;
                $row['selected'] = false;
            }

            $data = ArrayHelper::itemsMerge($data);
        }
        return $data;
    }

    /**
     * 获取渠道ID递归父级返回数组【[level1, level2, level3]】
     * @param int $channelId
     * @return LinkedcareChannel[]
     */
    public static function getChannelTreeList(int $channelId): array
    {
        $treeData = [];
        if ($channelId == 0) {
            return $treeData;
        }
        $channel = LinkedcareChannel::findOne($channelId);
        if (empty($channel)) {
            return $treeData;
        }
        $pChannel = self::getChannelTreeList($channel->parent_id);
        return array_merge($pChannel, [$channel]);
    }
}
